//
//  XHRTopicItem.m
//  BuDeJie
//
//  Created by haoran on 16/4/12.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRTopicItem.h"

@implementation XHRTopicItem

-(CGFloat)cellHeight{

    // 如果已经计算过，就直接返回
    if (_cellHeight) return _cellHeight;
    _cellHeight = 0;
    // 文字的Y值
    _cellHeight += 55;
    
    // 文字的高度
    CGSize textMaxSize = CGSizeMake(XHRScreenW - 2 * XHRMargin, MAXFLOAT);
    NSDictionary * attr = @{NSFontAttributeName : [UIFont systemFontOfSize:15]};
    _cellHeight += [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.height + XHRMargin;
    
    // 中间内容
    if (self.type != XHRTopicTypeWord) { // 非段子，可能是图片、声音、视频帖子
        CGFloat middleW = textMaxSize.width;
        CGFloat middleH = middleW * self.height / self.width;
        if (middleH >= XHRScreenH) { // 超过一个屏幕，就算是超长图片
            middleH = XHRScreenH * 0.3;
            self.bigPicture = YES;
        }
        
        CGFloat middleX = XHRMargin;
        CGFloat middleY = _cellHeight;
        self.middleFrame = CGRectMake(middleX, middleY, middleW, middleH);
        _cellHeight += XHRMargin + middleH;
        
        
        
    }
    // 最热评论
    if (self.top_cmt.count) {
        _cellHeight += 21; // 最热评论4个字的Label;
        
        // 计算评论内容的高度
        NSString * userName = self.top_cmt.firstObject[@"user"][@"username"];
        NSString * content = self.top_cmt.firstObject[@"content"];
        
        if (content.length == 0) { //语音评论
            content = @"语音评论";
        }
        NSString * topCmtContent = [NSString stringWithFormat:@"%@ : %@",userName,content];

        _cellHeight += [topCmtContent boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height + XHRMargin;
    }
    
    // 工具条的高度
    _cellHeight += 35 + XHRMargin;
    
    return _cellHeight;
}


@end
